package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.dao.ISceneryCommentDAO;
import cn.wolfcode.luowowo.comment.dao.ISceneryReplyDAO;
import cn.wolfcode.luowowo.comment.domain.SceneryComment;
import cn.wolfcode.luowowo.comment.domain.SceneryReply;
import cn.wolfcode.luowowo.comment.query.SceneryCommentQuery;
import cn.wolfcode.luowowo.comment.service.ISceneryCommentService;
import cn.wolfcode.luowowo.comment.service.ISceneryReplyService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class SceneryCommentServiceImpl implements ISceneryCommentService {

    @Autowired
    private ISceneryCommentDAO sceneryCommentDAO;

    @Autowired
    private ISceneryReplyDAO sceneryReplyDAO;

    @Autowired
    private MongoTemplate template;

    @Override
    public SceneryComment save(SceneryComment comment) {

        if (comment.getId() == null) {
            comment.setCreateTime(new Date());
        }

        sceneryCommentDAO.save(comment);

        return comment;
    }

    @Override
    public List<SceneryComment> list() {
        return sceneryCommentDAO.findAll(Sort.by(Sort.Direction.DESC, "createTime"));
    }

    @Override
    public SceneryComment get(String id) {
        Optional<SceneryComment> op = sceneryCommentDAO.findById(id);
        return op.isPresent() ? op.get() : null;
    }


    @Override
    public Page<SceneryComment> query(SceneryCommentQuery qo) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), sort);
        return sceneryCommentDAO.findAll(pageable);
    }

    @Override
    public Page<SceneryComment> findCommentBySceneryId(SceneryCommentQuery qo) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), sort);
        return sceneryCommentDAO.findBySceneryId(qo.getSceneryId(), pageable);
    }

    @Override
    public List<SceneryComment> queryByUserId(Long uid) {
        Query query = new Query();
        Criteria criteria = Criteria.where("userId").is(uid);
        Sort sort = null;
        sort = new Sort(Sort.Direction.DESC, "createTime");
        query.with(sort);
        query.addCriteria(criteria);
        List<SceneryComment> list = template.find(query, SceneryComment.class,"scenery_comment");
        return list;
    }

    @Override
    public boolean thumbUp(String commentid, Long fromid) {
        Optional<SceneryComment> optional = sceneryCommentDAO.findById(commentid);

        SceneryComment comment = optional.get();

        List<Long> thumbuplist = comment.getThumbuplist();
        //点赞
        boolean flag = false;
        if (!thumbuplist.contains(fromid)) {
            comment.setThumbupnum(comment.getThumbupnum() + 1);
            thumbuplist.add(fromid);
            flag = true;
        }else{
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            thumbuplist.remove(fromid);
        }
        comment.setThumbuplist(thumbuplist);
        this.save(comment);
        return flag;
    }

    @Override
    public SceneryComment getComment(String commentId) {
        SceneryComment comment = sceneryCommentDAO.findById(commentId).get();
        return comment;
    }
    @Override
    public List<SceneryReply> listForReply(String commentId) {

        Criteria criteria = Criteria.where("commentId").is(commentId);

        Query query = new Query();
        query.addCriteria(criteria);

        List<SceneryReply> list = template.find(query, SceneryReply.class, "scenery_reply");

        return list;
    }


}
